const Base = require('../base.js');

module.exports = class extends Base {
  /**
   * @api {POST} /conference 创建会议
   * @apiName add_conference
   * @apiGroup conference
   * @apiParam  {Number} pid 会议室id
   * @apiParam  {String} title 会议标题
   * @apiParam  {Number} type 远程会议0 本地会议1 其他可扩展
   * @apiParam  {String} proposer 申请人名称
   * @apiParam  {Number} number 参会人数
   * @apiParam  {Number} startDate 开始时间 UNIX时间戳
   * @apiParam  {Number} endDate 结束时间 UNIX时间戳
   * @apiParam  {String} content 会议内容
   * @apiSuccess (201) {Object} res 新增的会议对象
   * @apiVersion  1.0.0
   */
  async addAction() {
    const params = {
      pid: this.post('pid'),
      title: this.post('title'),
      type: this.post('type'),
      proposer: this.post('proposer'),
      number: this.post('number'),
      startDate: this.post('startDate'),
      endDate: this.post('endDate'),
      content: this.post('content')
    };
    const res = await this.model('conference', 'mysqlLocal').add(params);
    if (res) {
      params.id = res;
      this.status = 201;
      this.success(params, 'add ok');
    } else {
      this.fail(-1, 'add fail:' + res);
    }
  }

  /**
   * @api {PUT} /conference/:id 修改会议
   * @apiName edit_conference
   * @apiGroup conference
   * @apiParam  {Number} id 会议id
   * @apiParam  {Number} pid 会议室id
   * @apiParam  {String} title 会议标题
   * @apiParam  {Number} type 远程会议0 本地会议1 其他可扩展
   * @apiParam  {String} proposer 申请人名称
   * @apiParam  {Number} number 参会人数
   * @apiParam  {Number} startDate 开始时间 UNIX时间戳
   * @apiParam  {Number} endDate 结束时间 UNIX时间戳
   * @apiParam  {String} content 会议内容
   * @apiSuccess (201) {Object} res 修改后的会议对象
   * @apiVersion  1.0.0
   */
  async editAction() {
    const params = {
      pid: this.post('pid'),
      title: this.post('title'),
      type: this.post('type'),
      proposer: this.post('proposer'),
      number: this.post('number'),
      startDate: this.post('startDate'),
      endDate: this.post('endDate'),
      content: this.post('content')
    };
    const res = await this.model('conference', 'mysqlLocal').where({id: this.get('id')}).update(params);
    if (res) {
      params.id = this.get('id');
      this.status = 201;
      this.success(params, 'update ok');
    } else {
      this.fail(-1, 'update fail:' + res);
    }
  }

  /**
   * @api {DELETE} /conference/:id 删除会议
   * @apiName delete_conference
   * @apiGroup conference
   * @apiParam  {Number} id 会议id
   * @apiSuccess (204) {String} null 删除成功返回空文档
   * @apiVersion  1.0.0
   */
  async delAction() {
    const res = await this.model('conference', 'mysqlLocal').where({id: this.get('id')}).delete();
    if (res) {
      this.status = 204;
      this.success(res, 'delete ok');
    } else {
      this.fail(-1, 'delete fail:' + res);
    }
  }

  /**
   * @api {GET} /conference 获取会议列表
   * @apiName get_conference
   * @apiGroup conference
   * @apiSuccess (200) {Object[]} res 会议列表
   * @apiVersion  1.0.0
   */
  async listAction() {
    const res = await this.model('conference', 'mysqlLocal').select();
    if (res) {
      this.success(res, 'list ok');
    } else {
      this.fail(-1, 'list fail:' + res);
    }
  }
};
